/* MovieTheaterSimulation.c
 *	Main Movie Theater simulation Program.
 *	
 */

#include "syscall.h"

int TicketLineLock;
int TicketLineCustomerCondition;
int TicketClerkCustomerCondition;
int TicketClerkLock;
int TicketClerkCondition;

void TicketClerkThread() {
	Write("TicketClerk is running.\n", 25, ConsoleOutput);
	
	Acquire(TicketClerkLock);
	Wait(TicketClerkCondition, TicketClerkLock);
	
	Signal(TicketClerkCustomerCondition, TicketClerkLock);
	Write("TicketClerk giving ticket, waking up customer, and going to sleep.\n", 68, ConsoleOutput);
	Wait(TicketClerkCondition, TicketClerkLock);
	
	Write("TicketClerkThread finished.\n", 29, ConsoleOutput);
	Exit(0);
}

void HeadCustomerThread() {
	Write("HeadCustomer is running.\n", 26, ConsoleOutput);
	
	Acquire(TicketClerkLock);
	Signal(TicketClerkCondition, TicketClerkLock);
	Write("HeadCustomer waking up TicketClerk and waiting for ticket.\n", 60, ConsoleOutput);
	Wait(TicketClerkCustomerCondition, TicketClerkLock);
	Write("HeadCustomer got ticket. Entering the theater.\n", 48, ConsoleOutput);
	Release(TicketClerkLock);
	
	Write("HeadCustomerThread finished.\n", 30, ConsoleOutput);
	Exit(0);
}

int
main()
{
	TicketClerkLock = CreateLock("TicketClerkLock", 15);
	TicketClerkCondition = CreateCondition("TicketClerkCondition", 20);
	TicketClerkCustomerCondition = CreateCondition("TicketClerkCustomerCondition", 28);
	TicketLineLock = CreateLock("TicketLineLock", 14);
	TicketLineCustomerCondition = CreateCondition("TicketLineCustomerCondition", 27);
	
	Fork(TicketClerkThread);
	Fork(HeadCustomerThread);
	
	return 0;
}